
package com.compuvest.webstat;

// <editor-fold defaultstate="collapsed" desc="import">
import java.text.MessageFormat;

import java.util.Date;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
// </editor-fold>

public class WMail {

// <editor-fold defaultstate="collapsed" desc="string init">
    final static String PROMO_PRICE_MAIL =
        "Dear {0},\n\n<br/><br/>" +
        "We regret you did not finish your purchase with us.\n<br/>" +
        "We value your business and would like to offer this <font color='blue' size='+1'>CV{1}</font> coupon, " +
        "that you can use for the item you have selected in your original visit at our store.\n<br/>" +
        "This coupon is valid for 1 month, subject to product availability.\n<br/>" +
        "You can insert this coupon number into Promotion Code Field at your shopping basket.\n<br/>" +
        "<a href=\"http://www.compuvest.com/Desc.jsp?iid={2}\">{3}</a>\n<br/>" +
        "Original price ${4} Promotion price <font color='red' size='+1'>${5}</font> " +
        "you save <font color='red' size='+1'>{6}%</font>.\n<br/>" +
        "Limited QTY offer. While Supplies last.\n<br/>" +
        "Coupon cannot be combined with any other available offers, discounts or specials.\n<br/>" +
        "Please contact us for any exceptions that may apply.\n\n<br/><br/>" +
        "Sincerely,\n<br/>" +
        "Compuvest.com\n<br/>" +
        "Customer Care Team\n<br/>" +
        "888-644-7017\n<br/>" +
        "info@compuvest.com\n<br/>" +
        "www.compuvest.com\n<br/>";

    final static String DROP_PRICE_MAIL =
        "Dear {0},\n\n<br/><br/>" +
        "You had requested to be notified of a price-drop for one of our items.\n<br/>" +
        "We value your business and would like to offer this <font color='blue' size='+1'>CV{1}</font> coupon, " +
        "that you can use for the item you have selected in your original visit at our store.\n<br/>" +
        "This coupon is valid for 1 month, subject to product availability.\n<br/>" +
        "You can insert this coupon number into Promotion Code Field at your shopping basket.\n<br/>" +
        "<a href=\"http://www.compuvest.com/Desc.jsp?iid={2}\">{3}</a>\n<br/>" +
        "Original price ${4} Promotion price <font color='red' size='+1'>${5}</font> " +
        "you save <font color='red' size='+1'>{6}%</font>.\n<br/>" +
        "Limited QTY offer. While Supplies last.\n<br/>" +
        "Coupon cannot be combined with any other available offers, discounts or specials.\n<br/>" +
        "Please contact us for any exceptions that may apply.\n\n<br/><br/>" +
        "Sincerely,\n<br/>" +
        "Compuvest.com\n<br/>" +
        "Customer Care Team\n<br/>" +
        "888-644-7017\n<br/>" +
        "info@compuvest.com\n<br/>" +
        "www.compuvest.com\n<br/>";
// </editor-fold>

    private Session session;

// <editor-fold defaultstate="collapsed" desc="Mail()">

    public WMail() {

        // set mailserver
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        session = Session.getInstance(props,
                new javax.mail.Authenticator() {

                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("webinfo@compuvest.com", "2wsx3edc4rfv5tgb");
                    }
                });
        session.setDebug(false);

	System.out.println("WMailInit");
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="sendAproveReseller()">

    public void sendAproveReseller(String accwareAccount, String notes, String email) {

	StringBuilder body = new StringBuilder();
	body.append("\n<br/>AccwareAcount: ").append(accwareAccount);
	body.append("\n<br/>E-mail: ").append(email);
	body.append("\n<br/>Notice: ").append(notes);

	//            from:                    to:                           head                               body
	doSendEmail("info@compuvest.com", "vasil@compuvest.com", "Compuvest Corp. Approved Reseller " + email, body.toString(), "text/html");

	return;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="sendPromoPrice()">

    public void sendPromoPrice(
            String email,
            String promCode,
            String itemid,
            String itmName,
            String sellPrice,
            String promPrice,
            String percent,
            String expDate,
            String page) {

        String subject = "Coupon for unfinished order ";
        String formatBody = PROMO_PRICE_MAIL;
        if("drop".equals(page)) {
            subject = "Coupon for notified of a price-drop ";
            formatBody = DROP_PRICE_MAIL;
        }

        String body = MessageFormat.format(formatBody, new Object[] {
/*0*/      "Customer",
/*1*/      promCode,
/*2*/      itemid,
/*3*/      itmName,
/*4*/      sellPrice,
/*5*/      promPrice,
/*6*/      percent
        });


        try {
            final Message message = new MimeMessage(session);

    	    message.setFrom(new InternetAddress("rest@compuvest.com"));
    	    InternetAddress destsBCC[] = new InternetAddress[] {
                new InternetAddress("rest@compuvest.com"),
//                new InternetAddress("vasil@compuvest.com"),
                new InternetAddress(email)
            };

    	    message.setRecipients(Message.RecipientType.BCC, destsBCC);
    	    message.setSubject(subject + " with www.compuvest.com " + new Date());
            message.setContent(body, "text/html");

            new Thread() {
                @Override
                public void run() {
                    try {
                        Transport.send(message);
                    }
                    catch(MessagingException me) { System.out.println("WMail:sendPromo[Drop]Price Transport.send():\n" + me.getMessage()); }
                }
            }.start();
        }
        catch(Throwable st) { System.out.println("WMail:sendPromo[Drop]Price:\n" + st.fillInStackTrace()); }


	return;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="sendGoogleUpdateStatus()">

    public void sendGoogleUpdateStatus(Map<String,String> status) {

        if(status.isEmpty()) return;

        StringBuilder body = new StringBuilder();

        for(String accwareid : status.keySet()) {
            body.append(accwareid).append(" : ").append(status.get(accwareid)).append("\n<br/>");
        }

        try {
            final Message message = new MimeMessage(session);

    	    message.setFrom(new InternetAddress("info@compuvest.com"));
    	    InternetAddress destsBCC[] = new InternetAddress[] {
                new InternetAddress("vasil@compuvest.com"),
                new InternetAddress("dima@compuvest.com")
            };

    	    message.setRecipients(Message.RecipientType.BCC, destsBCC);
    	    message.setSubject("Google status " + new Date());
            message.setContent(body.toString(), "text/html");

            new Thread() {
                @Override
                public void run() {
                    try {
                        Transport.send(message);
                    }
                    catch(MessagingException me) { System.out.println("WMail:sendGoogleUpdateStatus() Transport.send()\n" + me.getMessage()); }
                }
            }.start();
        }
        catch(Throwable st) { System.out.println("WMail:sendGoogleUpdateStatus()\n" + st.fillInStackTrace()); }


	return;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="sendDiscountStatus()">
/*
    public void sendDiscountStatus(List<DiscountItem> items) {

        if(items.isEmpty()) return;

        StringBuilder body = new StringBuilder();

        for(DiscountItem item : items) {
            body.append(item.getResult()).append("\n<br/>");
        }

        try {
            final Message message = new MimeMessage(session);

    	    message.setFrom(new InternetAddress("info@compuvest.com"));
    	    InternetAddress destsBCC[] = new InternetAddress[] {
                new InternetAddress("vasil@compuvest.com"),
                new InternetAddress("marat@compuvest.com"),
                new InternetAddress("ali@compuvest.com")
            };

    	    message.setRecipients(Message.RecipientType.BCC, destsBCC);
    	    message.setSubject("Google update " + items.size() + " items " + new Date());
            message.setContent(body.toString(), "text/html");

            new Thread() {
                @Override
                public void run() {
                    try {
                        Transport.send(message);
                    }
                    catch(MessagingException me) { System.out.println("WMail:sendDiscountStatus() Transport.send()\n" + me.getMessage()); }
                }
            }.start();
        }
        catch(Throwable st) { System.out.println("WMail:sendDiscountStatus()\n" + st.fillInStackTrace()); }


	return;
    }
     * 
     */
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="sendTokenError()">

    public void sendTokenError(String body) {

        try {
            final Message message = new MimeMessage(session);

    	    message.setFrom(new InternetAddress("info@compuvest.com"));
    	    InternetAddress destsBCC[] = new InternetAddress[] {
                new InternetAddress("vasil@compuvest.com"),
                new InternetAddress("marat@compuvest.com"),
                new InternetAddress("ali@compuvest.com")
            };

    	    message.setRecipients(Message.RecipientType.BCC, destsBCC);
    	    message.setSubject("Google update Error " + new Date());
            message.setContent(body, "text/html");

            new Thread() {
                @Override
                public void run() {
                    try {
                        Transport.send(message);
                    }
                    catch(MessagingException me) { System.out.println("WMail:sendTokenError() Transport.send()\n" + me.getMessage()); }
                }
            }.start();
        }
        catch(Throwable st) { System.out.println("WMail:sendTokenError()\n" + st.fillInStackTrace()); }


	return;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="doSendEmail()">

    private void doSendEmail(String fromAddress, String toAddress, String head, String body, String mime) {

        try {
            final Message message = new MimeMessage(session);

    	    message.setFrom(new InternetAddress(fromAddress));
    	    InternetAddress destsBCC[] = new InternetAddress[] {
//                new InternetAddress("rest@compuvest.com"),
                new InternetAddress(toAddress)
            };

    	    message.setRecipients(Message.RecipientType.BCC, destsBCC);
    	    message.setSubject(head);
            message.setContent(body, mime);

            new Thread() {
                @Override
                public void run() {
                    try {
                        Transport.send(message);
                    }
                    catch(MessagingException me) { System.out.println("WMail::doSendMail Transport.send():\n" + me.getMessage()); }
                }
            }.start();
        }
        catch(Throwable st) { System.out.println("WMail::doSendMail::\n" + st.fillInStackTrace()); }

    }
// </editor-fold>

}
